﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
namespace HKEA.CMS.Core.DAL.App
{
    public class app_tblCPerson_JobNatureDAL:Iapp_tblCPerson_JobNature
    {
        HKEA_CMSEntities model;
        app_tblCPerson_JobNature jobnature;
        private app_tblCPerson_JobNatureDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static app_tblCPerson_JobNatureDAL GetInstance()
        {
            return new app_tblCPerson_JobNatureDAL();
        }

        public int InsertENT(app_tblCPerson_JobNature ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.app_tblCPerson_JobNature.AddObject(ent);
            if (model.SaveChanges() > 0)
                return model.app_tblCPerson_JobNature.Max(a=>a.AppCPerson_JobNatureUID);
            return 0;
        }

        public int InsertList(List<app_tblCPerson_JobNature> list)
        {
            foreach (var ent in list)
            {
                ent.CreationDate = DateTime.Now;
                ent.LastModifyDate = DateTime.Now;
                model.app_tblCPerson_JobNature.AddObject(ent);
            }
            return model.SaveChanges();
        }


        public List<app_tblCPerson_JobNature> GetListByENT(app_tblCPerson_JobNature ent, bool AddDeletedForSelect)
        {
            var ret = model.app_tblCPerson_JobNature.Where(a=>a.AppCPerson_JobNatureUID > 0);
            if (AddDeletedForSelect)
                ret = ret.Where(a => a.Deleted == ent.Deleted);
            if (ent.AppContactPersonUID > 0)
                ret = ret.Where(a=>a.AppContactPersonUID==ent.AppContactPersonUID);
            if (ent.ContactPersonUID > 0)
                ret = ret.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            return ret.ToList();
        }

        public int GetCountByENT(app_tblCPerson_JobNature ent)
        {
            throw new NotImplementedException();
        }

        public int UpdateENT(app_tblCPerson_JobNature ent)
        {
            var result = model.app_tblCPerson_JobNature.Where(a=>a.AppCPerson_JobNatureUID==ent.AppCPerson_JobNatureUID).FirstOrDefault();
            if (result != null)
            {
                result.AppContactPersonUID = ent.AppContactPersonUID;
                result.ContactPersonUID = ent.ContactPersonUID;
                result.CPerson_JobNatureUID = ent.CPerson_JobNatureUID;
                result.Deleted = ent.Deleted;
                result.JobNatureUID = ent.JobNatureUID;
                result.Remarks = ent.Remarks;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(app_tblCPerson_JobNature ent)
        {
            var result = model.app_tblCPerson_JobNature.Where(a=>a.AppCPerson_JobNatureUID==ent.AppCPerson_JobNatureUID).FirstOrDefault();
            if (result != null)
                model.app_tblCPerson_JobNature.DeleteObject(result);
            return model.SaveChanges();
        }


        public int DeleteByKeyList(List<int> list)
        {
            foreach (var temp in list)
            {
                var result = model.app_tblCPerson_JobNature.Where(a => a.AppCPerson_JobNatureUID == temp).FirstOrDefault();
                if (result != null)
                    model.app_tblCPerson_JobNature.DeleteObject(result);
            }
            return model.SaveChanges();
        }


        public int SaveList(List<int> ToDeletedKey, List<app_tblCPerson_JobNature> AddList, List<app_tblCPerson_JobNature> AddToDeletedList)
        {
            if (ToDeletedKey!=null && ToDeletedKey.Count > 0)
            {
                foreach (var temp in ToDeletedKey)
                {
                    var result = model.app_tblCPerson_JobNature.Where(a => a.AppCPerson_JobNatureUID == temp).FirstOrDefault();
                    if (result != null)
                        model.app_tblCPerson_JobNature.DeleteObject(result);
                }
            }
            if (AddList !=null && AddList.Count > 0)
            {
                foreach (var temp in AddList)
                {
                    temp.CreationDate = DateTime.Now;
                    temp.LastModifyDate = DateTime.Now;
                    model.app_tblCPerson_JobNature.AddObject(temp);
                }
            }
            if (AddToDeletedList!=null && AddToDeletedList.Count > 0)
            {
                foreach (var temp in AddToDeletedList)
                {
                    temp.CreationDate = DateTime.Now;
                    temp.LastModifyDate = DateTime.Now;
                    model.app_tblCPerson_JobNature.AddObject(temp);
                }
            }
            return model.SaveChanges();
        }
    }
}
